<?php

class SurveyController extends Controller
{

	public function actionRead()
	{	
		$modelSurvey = Survey::model()->findByPk($_GET['id']);
		$res = $modelSurvey->getQuestions();
		$res['survey'] = $modelSurvey->attributes;
		$this->responseJSON(0,$res);
	}

	public function actionCreate()
	{
		$modelSurvey = new Survey;
		$survey = $_POST;
		$modelSurvey->attributes = $survey;
		if($modelSurvey->save()){
			$survey_id = $modelSurvey->id;
			$this->responseJSON($modelSurvey->attributes);
		}else{
			echo "survey error";
		}	
	}

	public function _actionCreate()
	{
		$modelSurvey = new Survey;
		$modelQuestion = new Question;
		$modelOption = new Option;
		
		if(!isset($_POST['title'])){
			echo "no title";
			return;
		}
		if(!isset($_POST['questions'])){
			echo "no questions";
			return;
		}		
		
		$survey = $_POST;
		$modelSurvey->attributes = $survey;
		if($modelSurvey->save()){
			$survey_id = $modelSurvey->id;
			$questions = $survey['questions'];
			foreach ($questions as $key => $value) {
				$value['survey_id'] = $survey_id;
				$modelQuestion->attributes = $value;
				if($modelQuestion->save()){
					$question_id = $modelQuestion->id;
					$options = $value['options'];
					foreach ($options as $k => $v) {
						$v['survey_id'] = $survey_id;
						$v['question_id'] = $question_id;
						if($modelOption->save()){

						}else{
							echo "option error";
						}
					}
				}else{
					echo "question error";
				}
			}
		}else{
			echo "survey error";
		}
		
		if(isset($_POST['Survey']))
		{
			$model->attributes=$_POST['Survey'];
			if($model->save())
				echo "OK";
		}
	}

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST))
		{
			$model->attributes=$_POST;
			if($model->save())
				$this->responseJSON($model->attributes);
		}
	}

	public function actionDelete($id)
	{
		if($this->loadModel($id)->delete()){
			$this->responseJSON("success");
		};
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		//$dataProvider=new CActiveDataProvider('Survey');
		$dataProvider = new CActiveDataProvider('Survey', array(
		    'criteria' => array(
		        //'condition'=>'status=1',
		        //'order'=>'create_time DESC',
		        //'with'=>array('author'),
		    ),
		    'pagination' => array(
		        'pageSize' => 20,
    		),
		));

		$data = $dataProvider->getData();
		$res = array();
		foreach ($data as $survey) {
			$res[] = $survey->attributes;
		}
		$this->responseJSON($res);
	}

	public function actionQuestion()
	{
		$type = isset($_GET['type'])?trim($_GET['type']):"";
		switch ($type) {
			case 'create':
				$sid = $_POST['sid'];

				$question = $_POST;
				$options = array();
				if(isset( $question['options'])){
					$options = $question['options'];
					unset($question['options']);
				}
		
				$modelQuestion = new SurveyQuestion;
				$modelQuestion->setAttributes($question);
				$modelQuestion->setAttribute('survey_id',$sid);
				$modelQuestion->setAttribute('status',Survey::STATUS_OK);

				$modelQuestion->save();
				
				$qid = $modelQuestion->id;

				foreach ($options as $key => $option) {
					$modelOption = new SurveyQuestionOption;
					$modelOption->setAttributes($option);
					$modelOption->setAttribute('survey_id',$sid);
					$modelOption->setAttribute('survey_question_id',$qid);
					$modelOption->setAttribute('status',Survey::STATUS_OK);
					$modelOption->save();
				}
				$this->responseJSON(0,$modelQuestion->attributes);
				break;
			case 'update':
				$sid = $_POST['sid'];
				$qid = $_POST['qid'];
				$question = $_POST;
				$modelQuestion = SurveyQuestion::model()->findByPk($qid);
				$modelQuestion->setAttributes($question);
				$modelQuestion->setAttributes('survey_id',$sid);
				$modelQuestion->save();
				$this->responseJSON(0,$modelQuestion->attributes);
				break;
			case 'delete':
				$sid = $_POST['sid'];
				$qid = $_POST['qid'];
				$modelQuestion = SurveyQuestion::model()->findByPk($qid);
				$modelQuestion->setAttribute('status',Survey::STATUS_DELETED);
				$modelQuestion->save();
				$this->responseJSON(0,array());
				break;
			case 'read':
				$sid = $_GET['sid'];
				$qid = $_GET['qid'];
				$modelQuestion = SurveyQuestion::model()->findByPk($qid);
				$question = $modelQuestion->attributes;
				$question['options'] = array();
				$options = SurveyQuestionOption::model()->findAll('survey_question_id='.$question['id']);
				foreach ($options as $k => $v) {
					$question['options'][] = $v->attributes;
				}
				$this->responseJSON(0,$question);
				break;
			
			default:
				# code...
				break;
		}
	}

	public function actionOption()
	{
		$type = isset($_GET['type'])?trim($_GET['type']):"";
		switch ($type) {
			case 'create':
				$sid = $_POST['sid'];
				$qid = $_POST['qid'];
				$option = $_POST;
				$modelOption = new SurveyQuestionOption;
				$modelOption->setAttributes($option);
				$modelOption->setAttribute('survey_id',$sid);
				$modelOption->setAttribute('survey_question_id',$qid);
				$modelOption->setAttribute('status',Survey::STATUS_OK);
				$modelOption->save();
				$this->responseJSON(0,$modelOption->attributes);
				break;

			case 'update':
				$sid = $_POST['sid'];
				$qid = $_POST['qid'];
				$oid = $_POST['oid'];
				$option = $_POST;
				$modelOption = SurveyQuestionOption::model()->findByPk($oid);
				$modelOption->setAttributes($option);
				$modelOption->setAttributes('survey_id',$sid);
				$modelOption->setAttributes('survey_question_id',$qid);
				$modelOption->save();
				$this->responseJSON(0,$modelOption->attributes);
				break;

			case 'delete':
				$sid = $_POST['sid'];
				$qid = $_POST['qid'];
				$oid = $_POST['oid'];
				$modelOption = SurveyQuestionOption::model()->findByPk($oid);
				$modelOption->setAttribute('status',Survey::STATUS_DELETED);
				$modelOption->save();
				$this->responseJSON(0,array());
				break;

			case 'read':
				$sid = $_GET['sid'];
				$qid = $_GET['qid'];
				$oid = $_GET['oid'];
				$modelOption = SurveyQuestionOption::model()->findByPk($oid);
				$this->responseJSON(0,$modelOption->attributes);
				break;
			
			default:
				break;
		}
	}


	public function actionAdmin()
	{
		$model = new Survey('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Survey']))
			$model->attributes=$_GET['Survey'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	public function actionList()
	{
		$criteria = new CDbCriteria( array(
				'condition'=>'status=0',
				'order'=>'id DESC',
			) );
		
		$res = $this->createList("Survey", $criteria);
		foreach ($res['list'] as $key => $value) {
		}
		$this->responseJSON(0, $res);
	}

	public function loadModel($id)
	{
		$model=Survey::model()->findByPk($id);
		if($model===null)
			$this->responseJSON("404");
		return $model;
	}
}